//
//  SpotSelectDeliveryView.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/7/2.
//

import UIKit

class SpotSelectDeliveryView: UIView {

    typealias SpotDeliveryValueBlock = () -> Void
    var spotDeliveryValueBlock : SpotDeliveryValueBlock!
    var orderInfoModel:OrderListModel!
    var maxHeightValue:CGFloat = 0
    var minHeightValue:CGFloat = 0
    var mainViewOriginY:CGFloat = 0
    var selectBtn:UIButton!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.5)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardShow), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardHide), name: UIResponder.keyboardWillHideNotification, object: nil)
        maxHeightValue = 291
        minHeightValue = 211
        self.setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUI()  {
        self.addSubview(self.mainView)
        self.mainView.addSubview(self.cencelBtn)
        self.mainView.addSubview(self.titleLab)
        self.mainView.addSubview(self.confirmBtn)
        self.mainView.addSubview(self.visitBtn)
        self.mainView.addSubview(self.stationBtn)
        self.mainView.addSubview(self.airportBtn)
        self.mainView.addSubview(self.hintLab)
        self.mainView.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.left).offset(0);
            make.right.equalTo(self.snp.right).offset(0);
            make.bottom.equalTo(self.snp.bottom).offset(0);
            make.height.equalTo(228 + Constants.xSafeBottomHeight);
        }
        self.cencelBtn.snp.makeConstraints { (make) in
            make.height.equalTo(40);
            make.width.equalTo(40);
            make.top.equalTo(self.mainView.snp.top).offset(15);
            make.right.equalTo(self.snp.right).offset(-15);
        }
        self.titleLab.snp.makeConstraints { (make) in
            make.left.equalTo(self.mainView.snp.left).offset(15);
            make.top.equalTo(self.mainView.snp.top).offset(15);
        }
        self.confirmBtn.snp.makeConstraints { (make) in
            make.left.equalTo(self.mainView.snp.left).offset(60);
            make.right.equalTo(self.mainView.snp.right).offset(-60);
            make.bottom.equalTo(self.mainView.snp.bottom).offset(-(Constants.xSafeBottomHeight == 0 ? 8 : Constants.xSafeBottomHeight));
            make.height.equalTo(40);
        }
        self.visitBtn.snp.makeConstraints { (make) in
            make.top.equalTo(self.titleLab.snp.bottom).offset(30);
            make.left.equalTo(self.mainView.snp.left).offset(20);
            make.height.equalTo(40);
            make.width.equalTo((Constants.screenWidth - 80)/3);
        }
        self.stationBtn.snp.makeConstraints { (make) in
            make.top.equalTo(self.titleLab.snp.bottom).offset(30);
            make.centerX.equalTo(self.mainView.snp.centerX);
            make.width.equalTo((Constants.screenWidth - 80)/3);
            make.height.equalTo(self.visitBtn.snp.height);
        }
        self.airportBtn.snp.makeConstraints { (make) in
            make.top.equalTo(self.titleLab.snp.bottom).offset(30);
            make.right.equalTo(self.mainView.snp.right).offset(-20);
            make.width.equalTo((Constants.screenWidth - 80)/3);
            make.height.equalTo(self.visitBtn.snp.height);
        }
        self.hintLab.snp.makeConstraints { (make) in
            make.left.equalTo(self.mainView.snp.left).offset(15);
            make.top.equalTo(self.visitBtn.snp.bottom).offset(6);
        }
        self.setValueUI()
    }
    
    func setValueUI() {
        titleLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_shippingMethod)
        hintLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_hint_visit_exceed)
        confirmBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_confirm), for: .normal)
        visitBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_deliver_visit), for: .normal)
        stationBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_deliver_stationt), for: .normal)
        airportBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_deliver_airport), for: .normal)
    }

    func setOrderInfoModel(orderInfoModel:OrderListModel) {
        self.orderInfoModel = orderInfoModel
        selectBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        selectBtn.backgroundColor = UIColor.white
        selectBtn.layer.borderColor = UIColor.init(hexString: "#B6B6B6").cgColor
        if orderInfoModel.supportDelivery {
            visitBtn.setTitleColor(UIColor.init(hexString: "#B6B6B6"), for: .normal)
            visitBtn.isUserInteractionEnabled = false
            hintLab.isHidden = false
        } else {
            visitBtn.setTitleColor(UIColor.init(hexString: "#000000"), for: .normal)
            visitBtn.isUserInteractionEnabled = true
            hintLab.isHidden = true
        }
        if orderInfoModel.deliveryWay == 2 {
            selectBtn = airportBtn
        } else if orderInfoModel.deliveryWay == 1 {
            selectBtn = stationBtn
        } else {
            selectBtn = visitBtn
        }
        selectBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
        selectBtn.backgroundColor = UIColor.init(red: 255/255.0, green: 209/255.0, blue: 10/255.0, alpha: 0.28)
        selectBtn.layer.borderColor = UIColor.init(hexString: "#FFD10A").cgColor
        if selectBtn == visitBtn {
            calculateTextV.removeFromSuperview()
            self.mainView.snp.updateConstraints { (make) in
                make.height.equalTo(minHeightValue + Constants.xSafeBottomHeight)
            }
            calculateTextV.isHidden = true
        } else {
            self.mainView.addSubview(self.calculateTextV)
            self.mainView.snp.updateConstraints { (make) in
                make.height.equalTo(maxHeightValue + Constants.xSafeBottomHeight)
            }
            calculateTextV.snp.makeConstraints { (make) in
                make.left.equalTo(self.mainView.snp.left).offset(20);
                make.right.equalTo(self.mainView.snp.right).offset(-20);
                make.top.equalTo(self.hintLab.snp.bottom).offset(15);
                make.bottom.equalTo(self.confirmBtn.snp.top).offset(-25);
            }
            calculateTextV.isHidden = false
        }
    }
    
    @objc func clickCancelBtn() {
        self.removeFromSuperview()
    }
    
    @objc func clickConfirmBtn()  {
        if selectBtn == nil {
            Constants.window?.makeToast(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_deliver_way))
            return
        }
        if selectBtn.tag != 120 {
            orderInfoModel.payType = 0
            if selectBtn.tag == 121 {
                orderInfoModel.deliveryWay = 1
                orderInfoModel.deliveryDetailAddress = calculateTextV.textView.text
            } else if selectBtn.tag == 122 {
                orderInfoModel.deliveryWay = 2
                orderInfoModel.deliveryDetailAddress = calculateTextV.textView.text
            }
        } else {
            orderInfoModel.deliveryWay = 0
        }
        selectBtn.layer.borderWidth = 0.5
        //选择送至车站和送至机场的时候把 清空可选附加服务模块数据
        //支付方式 需要变成寄付现结
        //时间 修改为标准时间
        if orderInfoModel.deliveryWay == 1 || orderInfoModel.deliveryWay == 2 {
            orderInfoModel.collectionFreightAmount = ""
            orderInfoModel.insureFreight = 0
            orderInfoModel.insureAmount = ""
            if orderInfoModel.payType == 2 {
                orderInfoModel.payType = 0
            }
        }
        if (spotDeliveryValueBlock != nil) {
            spotDeliveryValueBlock()
        }
        self.removeFromSuperview()
    }
    
    @objc func clickSelectDeliver(sender:UIButton) {
        if selectBtn == sender {
            return
        }
        selectBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        selectBtn.backgroundColor = UIColor.white
        selectBtn.layer.borderColor = UIColor.init(hexString: "#B6B6B6").cgColor
        selectBtn = sender
        if sender.tag == 120 {
            calculateTextV.removeFromSuperview()
            UIView.animate(withDuration: 1.0) {
                self.mainView.snp.updateConstraints { (make) in
                    make.height.equalTo(self.minHeightValue + Constants.xSafeBottomHeight);
                }
            }
            calculateTextV.isHidden = true
        } else if sender.tag == 122 {
            self.mainView.addSubview(self.calculateTextV)
            UIView.animate(withDuration: 1.0) {
                self.mainView.snp.updateConstraints { (make) in
                    make.height.equalTo(self.maxHeightValue + Constants.xSafeBottomHeight);
                }
                self.calculateTextV.snp.makeConstraints { (make) in
                    make.left.equalTo(self.mainView.snp.left).offset(20);
                    make.right.equalTo(self.mainView.snp.right).offset(-20);
                    make.top.equalTo(self.hintLab.snp.bottom).offset(15);
                    make.bottom.equalTo(self.confirmBtn.snp.top).offset(-25);
                }
            }
            calculateTextV.isHidden = false
        } else {
            self.mainView.addSubview(self.calculateTextV)
            UIView.animate(withDuration: 1.0) {
                self.mainView.snp.updateConstraints { (make) in
                    make.height.equalTo(self.maxHeightValue + Constants.xSafeBottomHeight);
                }
                self.calculateTextV.snp.makeConstraints { (make) in
                    make.left.equalTo(self.mainView.snp.left).offset(20);
                    make.right.equalTo(self.mainView.snp.right).offset(-20);
                    make.top.equalTo(self.hintLab.snp.bottom).offset(15);
                    make.bottom.equalTo(self.confirmBtn.snp.top).offset(-25);
                }
            }
            calculateTextV.isHidden = false
        }
        
        selectBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
        selectBtn.backgroundColor = UIColor.init(red: 255/255.0, green: 209/255.0, blue: 10/255.0, alpha: 0.28)
        selectBtn.layer.borderColor = UIColor.init(hexString: "#FFD10A").cgColor
    }
    
    @objc func keyboardShow(notification:Notification)  {
        UIView.animate(withDuration: 2) {
            var frame:CGRect = self.mainView.frame;
            frame.origin.y = Constants.screenHeight - (self.mainView.frame.size.height) - 280;
            self.mainView.frame = frame;
        }
    }
    
    @objc func keyboardHide(notification:Notification)  {
        UIView.animate(withDuration: 2) {
            var frame:CGRect = self.mainView.frame;
            frame.origin.y = Constants.screenHeight - (self.mainView.frame.size.height);
            self.mainView.frame = frame;
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        self.endEditing(true)
    }
    
    lazy var mainView: UIView = {
        let mainView = UIView()
        mainView.backgroundColor = UIColor.white
        mainView.layer.masksToBounds = true
        mainView.layer.cornerRadius = 15
        return mainView
    }()
    lazy var cencelBtn: UIButton = {
        let cencelBtn = UIButton.init(type: .custom)
        cencelBtn.setImage(UIImage.init(named: "login_cancel"), for: .normal)
        cencelBtn.addTarget(self, action: #selector(clickCancelBtn), for: .touchUpInside)
        return cencelBtn
    }()
    lazy var titleLab: UILabel = {
        let titleLab = UILabel()
        titleLab.font = UIFont.boldSystemFont(ofSize: 17)
        titleLab.textColor  = UIColor.init(hexString: "#4C4C4C")
        return titleLab
    }()
    lazy var confirmBtn: UIButton = {
        let confirmBtn = UIButton.init(type: .custom)
        confirmBtn.addTarget(self, action: #selector(clickConfirmBtn), for: .touchUpInside)
        confirmBtn.backgroundColor = UIColor.init(hexString: "#FFD10A")
        confirmBtn.layer.masksToBounds = true
        confirmBtn.layer.cornerRadius = 20
        confirmBtn.setTitleColor(UIColor.init(hexString: "#000000"), for: .normal)
        confirmBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        return confirmBtn
    }()
    lazy var visitBtn: UIButton = {
        let visitBtn = UIButton.init(type: .custom)
        visitBtn.addTarget(self, action: #selector(clickSelectDeliver), for: .touchUpInside)
        visitBtn.backgroundColor = UIColor.white
        visitBtn.layer.borderColor = UIColor.init(hexString: "#B6B6B6").cgColor
        visitBtn.layer.masksToBounds = true
        visitBtn.layer.cornerRadius = 20
        visitBtn.tag = 120
        visitBtn.layer.borderWidth = 0.5
        visitBtn.setTitleColor(UIColor.init(hexString: "#000000"), for: .normal)
        visitBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        return visitBtn
    }()
    lazy var stationBtn: UIButton = {
        let stationBtn = UIButton.init(type: .custom)
        stationBtn.addTarget(self, action: #selector(clickSelectDeliver), for: .touchUpInside)
        stationBtn.backgroundColor = UIColor.white
        stationBtn.layer.borderColor = UIColor.init(hexString: "#B6B6B6").cgColor
        stationBtn.layer.masksToBounds = true
        stationBtn.layer.cornerRadius = 20
        stationBtn.tag = 121
        stationBtn.layer.borderWidth = 0.5
        stationBtn.setTitleColor(UIColor.init(hexString: "#000000"), for: .normal)
        stationBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        stationBtn.titleLabel?.textAlignment = .center
        stationBtn.titleLabel?.numberOfLines = 0;
        return stationBtn
    }()
    lazy var airportBtn: UIButton = {
        let airportBtn = UIButton.init(type: .custom)
        airportBtn.addTarget(self, action: #selector(clickSelectDeliver), for: .touchUpInside)
        airportBtn.backgroundColor = UIColor.white
        airportBtn.layer.borderColor = UIColor.init(hexString: "#B6B6B6").cgColor
        airportBtn.layer.masksToBounds = true
        airportBtn.layer.cornerRadius = 20
        airportBtn.tag = 122
        airportBtn.layer.borderWidth = 0.5
        airportBtn.setTitleColor(UIColor.init(hexString: "#000000"), for: .normal)
        airportBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        airportBtn.titleLabel?.textAlignment = .center
        airportBtn.titleLabel?.numberOfLines = 0;
        return airportBtn
    }()
    lazy var hintLab: UILabel = {
        let hintLab = UILabel()
        hintLab.font = UIFont.systemFont(ofSize: 10)
        hintLab.textColor = UIColor.init(hexString: "#FA1212")
        hintLab.isHidden = true
        return hintLab
    }()
    lazy var calculateTextV: SpotCalculateTextView = {
        let calculateTextV:SpotCalculateTextView = Bundle.main.loadNibNamed("SpotCalculateTextView", owner: self, options: nil)?.first as! SpotCalculateTextView
//        calculateTextV.textView.placeholder = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_hint_write_scene)
        calculateTextV.setTextMaxCount(textMaxCount: 100)
        return calculateTextV
    }()
}
